package org.tio.sitexxx.web.server.controller.gl;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.*;
import org.tio.sitexxx.service.service.gl.MemberManageService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.web.server.controller.minip.kf_order;
import org.tio.utils.resp.Resp;

import java.net.HttpRetryException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestPath("/gl/manage/member")
public class MemberManage {
    private static Logger log = LoggerFactory.getLogger(kf_order.class);

    /**
     * 会员列表
     *
     * @param pageNumber
     * @param pageSize   type =1 返回未参加形象测试的member
     *                   type=2 返回参加过形象测试并顾问没有回答过的member
     *                   type =3返回参加过形象测试并顾问回答过的member
     * @return
     */
    @RequestPath("/member-list")
    public Resp memberList(Integer pageNumber, Integer pageSize, Integer type) {
        if (type == null || pageNumber == null || pageSize == null) return Resp.fail("参数错误");
        if (type == 1) {
            Page<Member> memberPage = MemberManageService.me.memberListNotTest(pageNumber, pageSize);
            return Resp.ok(memberPage);
        } else if (type == 2) {
            Page<Record> recordPage = MemberManageService.me.memberListTest(pageNumber, pageSize);
            return Resp.ok(recordPage);
        }
        Page<Record> memberPage = MemberManageService.me.memberListTested(pageNumber, pageSize);
        if (memberPage == null)
            return Resp.ok("没有数据了。。。");
        return Resp.ok(memberPage);
    }

    /**
     * 根据会员的id查询订单列表
     */
    @RequestPath("/paymemt-list")
    public Resp questionList(Integer id, Integer pageNumber, Integer pageSize) {
        if (id == null) return Resp.fail();
        Page<Record> payments = MemberManageService.me.questionList(id, pageNumber, pageSize);
        if (payments == null || payments.getList().size() == 0)
            return Resp.fail();
        return Resp.ok(payments);
    }

    /**
     * member信息（问题信息）
     */
    @RequestPath("/person-information")
    public Resp memberInformation(Integer id) {
        List<Imagetesting> imagetestings = MemberManageService.me.getMemberInformationById(id);
        if (imagetestings == null || imagetestings.size() == 0)
            return Resp.fail();
        return Resp.ok(imagetestings);
    }

    /**
     * 私人衣橱
     */
    @RequestPath("/person/wardrobe")
    public Resp personWardrobe(Integer id) {
        List<Wardrobe> wardrobes = MemberManageService.me.getWardrobeByMemberId(id);
        if (wardrobes == null || wardrobes.size() == 0)
            return Resp.fail();
        return Resp.ok(wardrobes);
    }

    /**
     * 查找用户(手机号)
     */

    @RequestPath("/find-member/mobile")
    public Resp findMemberByMobile(String mobile, Integer type) {
        if (null == mobile || type == null) return Resp.fail("手机号码不能为空");
        List<Map<String, Object>> members = MemberManageService.me.findMemberByMobile(mobile, type);
        if (members == null || members.size() == 0)
            return Resp.fail();
        return Resp.ok(members);
    }

    /**
     * 查找用户(用户名)
     */
    @RequestPath("/find-member/username")
    public Resp findMemberByUsername(String username, Integer type) {
        if (null == username || type == null) return Resp.fail("用户名不能为空");
        List<Map<String, Object>> members = MemberManageService.me.findMemberByUsername(username, type);
        if (members == null || members.size() == 0)
            return Resp.fail();
        return Resp.ok(members);
    }

    /**
     * 方案，问题详情 已经回答过的问题才请求这个接口
     *
     * @param id 问题id
     * @return
     */
    @RequestPath("/query/question")
    public Resp queryDesignByDesignId(Integer id) {
        Resp resp = Resp.ok();
        Map<String, Object> name = MemberManageService.me.findname(id);
        Map<String, Object> map = MemberManageService.me.findDesginByQuestionId(id);
        if (map.isEmpty() && name.isEmpty())//有些老的数据 顾问id 已经不存在了
            return Resp.fail();
        map.put("name", name.get("name"));
        System.out.println("result:" + JSON.toJSONString(map));
        resp.setData(map);
        return resp;
    }

    /**
     * 顾问评价用户衣橱，给出
     *
     * @param request
     * @return
     */
    @RequestPath("/wardrobe/modify")
    public Resp modify_wardrobe(HttpRequest request) {
        Map<String, Object> map = JSON.parseObject(request.getBodyString());
        try {
            Integer wardrobeId = Integer.valueOf(map.get("id").toString());
            String style = String.valueOf(map.get("style"));
            Integer suit = Integer.valueOf(map.get("suitable").toString());
            if ("null".equals(style)) return Resp.fail("style不能为空");
            boolean suitable = (suit == 1);
            System.out.println("wardrobeId:" + wardrobeId);
            Wardrobe wardrobe = Wardrobe.dao.findById(wardrobeId);
            if (null != wardrobe) {
                wardrobe.setStyle(style);
                wardrobe.setSuitable(suitable);
                boolean result = wardrobe.update();
                if (result) return Resp.ok("评价成功");
            }
        } catch (RuntimeException e) {
            if (e instanceof NullPointerException) {
                return Resp.fail("参数错误");
            }
            log.error(e.toString(), e);
            throw e;
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Resp.fail("未知错误");
        }
        return Resp.fail();
    }
}
